<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-9-23
 * Time: 下午5:41
 */
class UserController extends Controller {

    private $_logininfo;
    private $_daytime;
    private $_daydate;
    private $_title;

    public function init() {
        parent::init();
        $this->_logininfo = $this->_session->offsetGet('loginInfo');

        if(!$this->_session->has('loginInfo')) {
            $this->redirect('/login');
            die();
        }
        $__now_time = $_SERVER['REQUEST_TIME'];
        $this->_daytime = date("Y-m-d H:i:s",$__now_time);
        $this->_daydate = date("Y-m-d",$__now_time);
    }

    //会员中心
    public function indexAction(){
        $this->_title ="会员中心";
        $this->_view->title = $this->_title;

        global $_PurchaseOrderCfg,$_ServiceCfg;
        define('ITEM_PER_PAGE', 10);

        //获取用户信息
        $user = new UserModel();
        $userinfo = $user->getListById($this->_logininfo['id']);


        if(!empty($userinfo['worker_id'])){
            //客户经理
            $url = _HTTP_CLOUD_HOST."/api/getworker/getWorkerInfoByUserID?worker_id=".$userinfo['worker_id'];

            $worker = Tools::https_request($url,$data=array('v'=>'1'));
            $worker_info = json_decode($worker,true);
            $worker_info['personpic'] ="http://cloud.sumibuy.com/upload/".$worker_info['personpic'];
        }else{
            $worker_info['realname'] = $_ServiceCfg['realname'];
            $worker_info['phone'] = $_ServiceCfg['phone'];
            $worker_info['mobile'] = $_ServiceCfg['mobile'];
            $worker_info['qq'] = $_ServiceCfg['qq'];
            $worker_info['personpic'] = $_ServiceCfg['personpic'];
        }

        //最新报价
        $start=1;
        $where ="iq_inquiry.status='{$_PurchaseOrderCfg['deal']}'";
        $order ="iq_inquiry.update_time DESC";
        $buy = new BuyModel();
        $buylist = $buy->getPageListByWhere($start,ITEM_PER_PAGE,$where,$order);
        foreach($buylist as $key=>$val){
            $buylist[$key]['update_time'] = Tools::time2Units(time () - strtotime ( $val['update_time'] )) . "前";
        }
        $this->_view->user_info =$userinfo;
        $this->_view->buylist = $buylist;
        $this->_view->worker_info = $worker_info;
    }

    //发布报价
    public function supplyAction(){
        $this->_title ="发布报价";
        $this->_view->title = $this->_title;
        if($this->_request->isPost()){
            global $_suppliyStateCfg;
            $Post = $this->_request->getPost();

            $Post['remark'] = empty($Post['remark']) ? null : htmlspecialchars(trim($Post['remark']), ENT_QUOTES, 'UTF-8');
            $Post['business_id'] = empty($Post['business_id']) ? null : (int)trim($Post['business_id']);
            if(empty($Post['remark'])){
                $retdata = array(
                    'code' => '3',
                    'msg'  => '您的报价不能为空！'
                );
                echo json_encode($retdata);
                die();
            }
            $status = $_suppliyStateCfg['audit'];
            $data = array(
                'user_id'          => $this->_logininfo['id'],
                'business_id'      => $Post['business_id'],
                'remark'           => $Post['remark'],
                'organization_id'  => 1,
                'department_id'    => 1,
                'create_time'      => $this->_daytime,
                'create_uid'       => 1,
                'update_time'      => $this->_daytime,
                'update_uid'       => 1,
                'type'             => 'outer',
                'status'           => $status,
            );


            $suppliy = new QuotationModel();
            $ret = $suppliy->add($data);

            if(!empty($ret)){
                $retdata = array(
                    'code' => '8',
                    'msg'  => '报价发布成功！请待审核！'
                );
                echo json_encode($retdata);
                die();
            }else{
                $retdata = array(
                    'code' => '7',
                    'msg'  => '报价发布不成功！'
                );
                echo json_encode($retdata);
                die();
            }
        }

        $invoice = new UsersinvoiceModel();
        $info = $invoice->getListByRow("user_id",$this->_logininfo['id'],$orderby = "id desc");

        $this->_view->info = $info;
    }

    //发布求购
    public function buyAction(){
        $this->_title ="发布求购";
        $this->_view->title = $this->_title;
        if($this->_request->isPost()){
            global $_BuyStateCfg;
            $Post = $this->_request->getPost();

            $Post['remark'] = empty($Post['remark']) ? null : htmlspecialchars(trim($Post['remark']), ENT_QUOTES, 'UTF-8');
            $Post['business_id'] = empty($Post['business_id']) ? null : (int)trim($Post['business_id']);
            if(empty($Post['remark'])){
                $retdata = array(
                    'code' => '3',
                    'msg'  => '您的求购内容不能为空！'
                );
                echo json_encode($retdata);
                die();
            }
            $status = $_BuyStateCfg['wait_audit'];
            $data = array(
                'user_id'          => $this->_logininfo['id'],
                'business_id'      => $Post['business_id'],
                'srouce_type'      => 'M',
                'remark'           => $Post['remark'],
                'organization_id'  => 1,
                'department_id'    => 1,
                'create_time'      => $this->_daytime,
                'create_uid'       => 1,
                'update_time'      => $this->_daytime,
                'update_uid'       => 1,
                'type'             => 'require',
                'status'           => $status,
            );


            $suppliy = new BuyModel();
            $ret = $suppliy->add($data);

            if(!empty($ret)){
                $retdata = array(
                    'code' => '8',
                    'msg'  => '求购发布成功！请待审核！'
                );
                echo json_encode($retdata);
                die();
            }else{
                $retdata = array(
                    'code' => '7',
                    'msg'  => '求购发布不成功！'
                );
                echo json_encode($retdata);
                die();
            }
        }

        $invoice = new UsersinvoiceModel();
        $info = $invoice->getListByRow("user_id",$this->_logininfo['id'],$orderby = "id desc");

        $this->_view->info = $info;
    }

    //报价管理
    public function supplylistAction(){
        $this->_title ="报价管理";
        $this->_view->title = $this->_title;
        global $_suppliyStateMean,$_suppliyStateCfg;
        define ( 'ITEM_PER_PAGE', 10 );
        $pageurl="/user/supplylist";

        $condition = array();
        $quota = new QuotationModel();

        $status = $this->_request->getParam('status','all');

        //筛选条件
        if(!empty($status)){
            if($status == 'all'){
                $condition[] = "iq_quotation.status in({$_suppliyStateCfg['audit']},{$_suppliyStateCfg['up']})";
            }elseif($status == 'audit'){
                $condition[] = "iq_quotation.status = '{$_suppliyStateCfg['audit']}'";
            }elseif($status == 'up'){
                $condition[] = "iq_quotation.status = '{$_suppliyStateCfg['up']}'";
            }else{
                $condition[] = "iq_quotation.status in({$_suppliyStateCfg['down']},{$_suppliyStateCfg['cancel']})";
            }
            $pageurl .="/status/".$status;
        }


        $condition[] = "iq_quotation.type = 'outer' and iq_quotation.user_id ='{$this->_logininfo['id']}' ";

        if(!empty($condition))
        {
            $where = implode('AND ', $condition);

        }else{
            $where = " 1=1 ";
        }


        //排序
        $orderby ="iq_quotation.update_time DESC";


        $totalItems = $quota->getCountByWhere($where);
        // 获取分页开始
        $page = $this->_request->getParam ( "page", 1 );
        $itempage = $this->_request->getParam ( "itempage", 0 );

        $offset = ($page - 1) * ITEM_PER_PAGE;
        if ($offset >= $totalItems) {
            $page = ceil ( $totalItems / ITEM_PER_PAGE );
            $offset = ($page - 1) * ITEM_PER_PAGE;
        }
        if ($offset < 0) {
            $offset = 0;
            $page = 1;
        }
        $list = $quota->getPageListByWhere($page,ITEM_PER_PAGE, $where,$orderby);

        if(!empty($page)){
            $pageurl .= "/page/%d";
        }

        foreach($list as $key=>$var){
            $list[$key]['status'] = $_suppliyStateMean[$var['status']];
        }

        $links = Tools::createPage($pageurl, ITEM_PER_PAGE, $page, $totalItems, $target = '_self');

        $this->_view->links = $links;
        $this->_view->list = $list;
        $this->_view->status = $status;

    }

    //求购管理
    public function buylistAction(){
        $this->_title ="求购管理";
        $this->_view->title = $this->_title;
        global $_BuyStartMean,$_BuyStateCfg;
        define ( 'ITEM_PER_PAGE', 10 );
        $pageurl="/user/buylist";
        $condition = array();
        $buy = new BuyModel();

        $status = $this->_request->getParam('status','all');


        if(!empty($status)){
            if($status == 'all'){
                $condition[] = "iq_inquiry.status in({$_BuyStateCfg['audit']},{$_BuyStateCfg['wait_audit']},{$_BuyStateCfg['Negotiate']},{$_BuyStateCfg['Trading']},{$_BuyStateCfg['un_pay']},{$_BuyStateCfg['done']},{$_BuyStateCfg['cancel']}) ";
            }elseif($status == 'wait_audit'){
                $condition[] = "iq_inquiry.status = '{$_BuyStateCfg['wait_audit']}' ";
            }elseif($status == 'audit'){
                $condition[] = "iq_inquiry.status = '{$_BuyStateCfg['audit']}' ";
            }elseif($status == 'Negotiate'){
                $condition[] = "iq_inquiry.status = '{$_BuyStateCfg['Negotiate']}' ";
            }elseif($status == 'Trading'){
                $condition[] = "iq_inquiry.status = '{$_BuyStateCfg['Trading']}' ";
            }elseif($status == 'un_pay'){
                $condition[] = "iq_inquiry.status = '{$_BuyStateCfg['un_pay']}' ";
            }elseif($status == 'done'){
                $condition[] = "iq_inquiry.status = '{$_BuyStateCfg['done']}' ";
            }else{
                $condition[] = "iq_inquiry.status in({$_BuyStateCfg['cancel']}) ";
            }
            $pageurl .="/status/".$status;
        }

        $condition[] = "iq_inquiry.type = 'require' and iq_inquiry.user_id= '{$this->_logininfo['id']}' ";

        if(!empty($condition))
        {
            $where = implode('AND ', $condition);

        }else{
            $where = " 1=1 ";
        }

        //排序
        $orderby ="update_time desc";


        $totalItems = $buy->getCountByWhere($where);
        // 获取分页开始
        $page = $this->_request->getParam ( "page", 1 );
        $itempage = $this->_request->getParam ( "itempage", 0 );

        $offset = ($page - 1) * ITEM_PER_PAGE;
        if ($offset >= $totalItems) {
            $page = ceil ( $totalItems / ITEM_PER_PAGE );
            $offset = ($page - 1) * ITEM_PER_PAGE;
        }
        if ($offset < 0) {
            $offset = 0;
            $page = 1;
        }

        if(!empty($page)){
            $pageurl .= "/page/%d";
        }

        $list = $buy->getPageListByWhere($page,ITEM_PER_PAGE, $where,$orderby);

        foreach($list as $key=>$var){
            $list[$key]['status'] = $_BuyStartMean[$var['status']];
        }


        $links = Tools::createPage($pageurl, ITEM_PER_PAGE, $page, $totalItems, $target = '_self');

        $this->_view->links = $links;

        $this->_view->list = $list;
        $this->_view->status = $status;
    }

    //报价管理详情
    public function supplylistdetailAction(){
        $this->_title ="报价详情";
        $this->_view->title = $this->_title;
        global $_currencyMark,$_currencyNoCfg,$_currencyNoMean,$_currencyMean,$_currencyMeanCfg,$_suppliyStateMean,$_suppliyStateCfg,$_ServiceStateMean,$_ServiceStateCfg;
        $id = $this->_request->getParam('id',0);
        if(empty($id)){
            $this->redirect ( '/user/supplylist' );
        }
        $supply = new QuotationModel();
        $supplydata = $supply->getListById("iq_quotation.id={$id}");

        $this->_view->supplydata=$supplydata;
        $this->_view->_currencyMark = $_currencyMark;
        $this->_view->_currencyNoCfg = $_currencyNoCfg;
        $this->_view->_currencyNoMean =$_currencyNoMean;
        $this->_view->_currencyMean = $_currencyMean;
        $this->_view->_currencyMeanCfg = $_currencyMeanCfg;
        $this->_view->_suppliyStateMean = $_suppliyStateMean;
        $this->_view->_suppliyStateCfg =$_suppliyStateCfg;
        $this->_view->_ServiceStateMean=$_ServiceStateMean;
        $this->_view->_ServiceStateCfg = $_ServiceStateCfg;
    }

    //求购管理详情
    public function buylistdetailAction(){
        $this->_title ="求购详情";
        $this->_view->title = $this->_title;
        global $_BidPurchaseMean,$_BuyStateCfg, $_BuyStartMean;
        $id = $this->_request->getParam('id',0);
        if(empty($id)){
            $this->redirect ( '/user//buylist' );
        }

        $buy = new BuyModel();
        $buydata = $buy->getListById("iq_inquiry.id ={$id}");

        $this->_view->buydata=$buydata;
        $this->_view->_BuyStartMean = $_BuyStartMean;
        $this->_view->_BidPurchaseMean = $_BidPurchaseMean;
        $this->_view->_BuyStateCfg = $_BuyStateCfg;
    }

    //报价意向单
    public function supplyintentAction(){
        $this->_title ="意向单";
        $this->_view->title = $this->_title;

        global $_BidPurchaseMean,$_BidPurchaseCfg;
        define ( 'ITEM_PER_PAGE', 10 );
        $pageurl="/user/supplyintent";

        $condition = array();
        $quota = new QuotationModel();

        $status = $this->_request->getParam('status','all');


        if(!empty($status)){
            if($status == 'all'){
                $condition[] = "iq_quotation.status in({$_BidPurchaseCfg['audit']},{$_BidPurchaseCfg['order']},{$_BidPurchaseCfg['deal']},{$_BidPurchaseCfg['overdue']})";
            }else{
                $condition[] = "iq_quotation.status in({$_BidPurchaseCfg['cancel']})";
            }
            $pageurl .="/status/".$status;
        }

        $condition[] = "iq_quotation.type = 'require' and iq_quotation.user_id ='{$this->_logininfo['id']}'";

        if(!empty($condition))
        {
            $where = implode('AND ', $condition);

        }else{
            $where = " 1=1 ";
        }


        //排序
        $orderby ="iq_quotation.update_time desc";


        $totalItems = $quota->getIntentOrderTotal($where);
        // 获取分页开始
        $page = $this->_request->getParam ( "page", 1 );
        $itempage = $this->_request->getParam ( "itempage", 0 );

        $offset = ($page - 1) * ITEM_PER_PAGE;
        if ($offset >= $totalItems) {
            $page = ceil ( $totalItems / ITEM_PER_PAGE );
            $offset = ($page - 1) * ITEM_PER_PAGE;
        }
        if ($offset < 0) {
            $offset = 0;
            $page = 1;
        }

        if(!empty($page)){
            $pageurl .= "/page/%d";
        }

        $list = $quota->getIntentOrderList($page,ITEM_PER_PAGE, $where,$orderby);

        foreach($list as $key=>$var){
            $list[$key]['status'] = $_BidPurchaseMean[$var['status']];
        }

        $links = Tools::createPage($pageurl, ITEM_PER_PAGE, $page, $totalItems, $target = '_self');

        $this->_view->links = $links;

        $this->_view->list = $list;
        $this->_view->status = $status;


    }

    //求购意向单
    public function buyintentAction(){
        $this->_title ="已收到的报价";
        $this->_view->title = $this->_title;
        global $_BidPurchaseMean,$_BidPurchaseCfg;
        define ( 'ITEM_PER_PAGE', 10 );
        $pageurl="/user/buyintent";

        $condition = array();
        $quota = new QuotationModel();

        $status = $this->_request->getParam('status','all');


        if(!empty($status)){
            if($status == 'all'){
                $condition[] = "iq_quotation.status in({$_BidPurchaseCfg['audit']},{$_BidPurchaseCfg['order']},{$_BidPurchaseCfg['deal']},{$_BidPurchaseCfg['overdue']})";
            }else{
                $condition[] = "iq_quotation.status in({$_BidPurchaseCfg['cancel']})";
            }
            $pageurl .="/status/".$status;
        }

        $condition[] = "iq_quotation.type = 'require' and iq_inquiry.user_id ='{$this->_logininfo['id']}'";

        if(!empty($condition))
        {
            $where = implode('AND ', $condition);

        }else{
            $where = " 1=1 ";
        }


        //排序
        $orderby ="iq_quotation.update_time desc";


        $totalItems = $quota->getIntentOrderTotal($where);
        // 获取分页开始
        $page = $this->_request->getParam ( "page", 1 );
        $itempage = $this->_request->getParam ( "itempage", 0 );

        $offset = ($page - 1) * ITEM_PER_PAGE;
        if ($offset >= $totalItems) {
            $page = ceil ( $totalItems / ITEM_PER_PAGE );
            $offset = ($page - 1) * ITEM_PER_PAGE;
        }
        if ($offset < 0) {
            $offset = 0;
            $page = 1;
        }

        if(!empty($page)){
            $pageurl .= "/page/%d";
        }


        $list = $quota->getIntentOrderList($page,ITEM_PER_PAGE, $where,$orderby);

        foreach($list as $key=>$var){
            $list[$key]['status'] = $_BidPurchaseMean[$var['status']];
        }


        $links = Tools::createPage($pageurl, ITEM_PER_PAGE, $page, $totalItems, $target = '_self');

        $this->_view->links = $links;

        $this->_view->list = $list;
        $this->_view->status = $status;

    }

    //报价意向单详情
    public function supplyintentdetailAction(){
        $this->_title ="意向单详情";
        $this->_view->title = $this->_title;
        global $_BidPurchaseMean,$_BidPurchaseCfg;
        $id = $this->_request->getParam('id',0);
        if(empty($id)){
            $this->redirect ( '/user//buylist' );
        }

        $quota = new QuotationModel();
        $supplyintentdetail = $quota->getIntentDetailByWhere("iq_quotation.id={$id}");


        $this->_view->_BidPurchaseCfg = $_BidPurchaseCfg;
        $this->_view->_BidPurchaseMean = $_BidPurchaseMean;
        $this->_view->supplyintentdetail =$supplyintentdetail;
    }

    //求购向单详情
    public function buyintentdetailAction(){
        $this->_title ="已收到的报价详情";
        $this->_view->title = $this->_title;
        global $_BidPurchaseMean,$_BidPurchaseCfg ;
        $id = $this->_request->getParam('id',0);
        if(empty($id)){
            $this->redirect ( '/user/buyintent' );
        }

        $quota = new QuotationModel();
        $supplyintentdetail = $quota->getIntentDetailByWhere("iq_quotation.id={$id}");

        $this->_view->_BidPurchaseMean = $_BidPurchaseMean;
        $this->_view->_BidPurchaseCfg  = $_BidPurchaseCfg;
        $this->_view->supplyintentdetail =$supplyintentdetail;
    }

    //销售单
    public function saleorderAction(){
        $this->_title ="我的销售订单";
        $this->_view->title = $this->_title;
        global $_SupplyOrderMean,$_SupplyOrderCfg;
        define ( 'ITEM_PER_PAGE', 10 );
        $pageurl="/user/saleorder";


        $condition = array();
        $sale = new SaleorderModel();

        $status = $this->_request->getParam('status','all');


        if(!empty($status)){
            if($status == 'all'){
                $condition[] = "sale_order.status in({$_SupplyOrderCfg['audit']},{$_SupplyOrderCfg['deal']},{$_SupplyOrderCfg['done']},{$_SupplyOrderCfg['cancel']})";
            }elseif($status == 'audit'){
                $condition[] = "sale_order.status in({$_SupplyOrderCfg['cancel']})";

            }elseif($status == 'deal'){
                $condition[] = "sale_order.status in({$_SupplyOrderCfg['cancel']})";

            }elseif($status == 'done'){
                $condition[] = "sale_order.status in({$_SupplyOrderCfg['cancel']})";

            }else{
                $condition[] = "sale_order.status in({$_SupplyOrderCfg['cancel']})";
            }
            $pageurl .="/status/".$status;
        }

        $condition[] = "iq_quotation.user_id ='{$this->_logininfo['id']}'";

        if(!empty($condition))
        {
            $where = implode('AND ', $condition);

        }else{
            $where = " 1=1 ";
        }

        //排序
        $orderby ="sale_order.update_time desc";


        $totalItems = $sale->getSaleOrderTotal($where);
        // 获取分页开始
        $page = $this->_request->getParam ( "page", 1 );
        $itempage = $this->_request->getParam ( "itempage", 0 );

        $offset = ($page - 1) * ITEM_PER_PAGE;
        if ($offset >= $totalItems) {
            $page = ceil ( $totalItems / ITEM_PER_PAGE );
            $offset = ($page - 1) * ITEM_PER_PAGE;
        }
        if ($offset < 0) {
            $offset = 0;
            $page = 1;
        }

        if(!empty($page)){
            $pageurl .= "/page/%d";
        }


        $list = $sale->getSaleOrderList($page,ITEM_PER_PAGE, $where,$orderby);

        foreach($list as $key=>$var){
            $list[$key]['status'] = $_SupplyOrderMean[$var['status']];
        }

        $links = Tools::createPage($pageurl, ITEM_PER_PAGE, $page, $totalItems, $target = '_self');

        $this->_view->links = $links;


        $this->_view->list = $list;
        $this->_view->status = $status;
    }

    //采购单
    public function purchaseorderAction(){
        $this->_title ="我的采购订单";
        $this->_view->title = $this->_title;
        global $_SupplyOrderMean,$_SupplyOrderCfg,$_saleOrderType;
        define ( 'ITEM_PER_PAGE', 10 );
        $pageurl="/user/purchaseorder";

        $condition = array();
        $sale = new SaleorderModel();

        $status = $this->_request->getParam('status','all');


        if(!empty($status)){
            if($status == 'all'){
                $condition[] = "sale_order.status in({$_SupplyOrderCfg['audit']},{$_SupplyOrderCfg['deal']},{$_SupplyOrderCfg['done']},{$_SupplyOrderCfg['cancel']})";
            }elseif($status == 'audit'){
                $condition[] = "sale_order.status in({$_SupplyOrderCfg['audit']})";

            }elseif($status == 'deal'){
                $condition[] = "sale_order.status in({$_SupplyOrderCfg['deal']})";

            }elseif($status == 'done'){
                $condition[] = "sale_order.status in({$_SupplyOrderCfg['done']})";

            }else{
                $condition[] = "sale_order.status in({$_SupplyOrderCfg['cancel']})";
            }
            $pageurl .="/status/".$status;
        }

        $condition[] = "sale_order.user_id ='{$this->_logininfo['id']}'";

        if(!empty($condition))
        {
            $where = implode('AND ', $condition);

        }else{
            $where = " 1=1 ";
        }

        //排序
        $orderby ="sale_order.update_time desc";


        $totalItems = $sale->getSaleOrderTotal($where);
        // 获取分页开始
        $page = $this->_request->getParam ( "page", 1 );
        $itempage = $this->_request->getParam ( "itempage", 0 );

        $offset = ($page - 1) * ITEM_PER_PAGE;
        if ($offset >= $totalItems) {
            $page = ceil ( $totalItems / ITEM_PER_PAGE );
            $offset = ($page - 1) * ITEM_PER_PAGE;
        }
        if ($offset < 0) {
            $offset = 0;
            $page = 1;
        }


        if(!empty($page)){
            $pageurl .= "/page/%d";
        }


        $list = $sale->getSaleOrderList($page,ITEM_PER_PAGE, $where,$orderby);

        foreach($list as $key=>$val){
            if(empty($val['business_name'])){
                if($val['buy_sku_type']==$_saleOrderType['Own']) {
                    $validate = array(
                        'id' => $val['buy_sku_id'],
                    );

                    $url = _HTTP_CLOUD_HOST . "/api/getprice/getQuoteById";
                    $prodcut = Tools::https_request($url, $validate);
                    $getlist = json_decode($prodcut, true);
                    if (!empty($getlist) && $getlist['status'] == 'success') {
                        $list[$key]['business_name'] = '上海塑米信息科技有限公司';
                        $list[$key]['contacts'] = $getlist['data']['realname'];
                        $list[$key]['mobile'] = $getlist['data']['mobile'];
                    } else {
                        $list[$key]['business_name'] = '上海塑米信息科技有限公司';
                        $list[$key]['contacts'] = "冯丽姣";
                        $list[$key]['mobile'] = "021-61657142";
                    }
                }else{

                    $quota = new QuotationModel();
                    $quotainfo = $quota->getListById("iq_quotation.id='{$val['buy_sku_id']}'");

                    $list[$key]['business_name'] = $quotainfo['business_name'];
                    $list[$key]['contacts'] = $quotainfo['contacts'];
                    $list[$key]['mobile'] = $quotainfo['mobile'];
                    $list[$key]['qq'] = $quotainfo['qq'];
                }
            }
        }


        $links = Tools::createPage($pageurl, ITEM_PER_PAGE, $page, $totalItems, $target = '_self');

        $this->_view->links = $links;
        $this->_view->_SupplyOrderMean =$_SupplyOrderMean;
        $this->_view->list = $list;
        $this->_view->status = $status;
    }

    //销售单详情
    public function saleorderdetailAction(){
        $this->_title ="我的销售订单详情";
        $this->_view->title = $this->_title;
        global $_saleOrderType,$_SupplyOrderMean,$_orderDeliveryTypeMean,$_SupplyOrderCfg;
        $id = $this->_request->getParam('id',0);
        if(empty($id)){
            $this->redirect ( '/user//buylist' );
        }

        $order = new SaleorderModel();
        $orderinfo = $order->getSaleOrderDetail("sale_order.id={$id}");

        $this->_view->_supplyOrderCfg = $_SupplyOrderCfg;
        $this->_view->_orderDeliveryTypeMean=$_orderDeliveryTypeMean;
        $this->_view->_SupplyOrderMean = $_SupplyOrderMean;
        $this->_view->orderinfo =$orderinfo;
    }

    //采购单详情
    public function purchaseorderdetailAction(){
        $this->_title ="我的采购订单详情";
        $this->_view->title = $this->_title;
        global $_saleOrderType,$_SupplyOrderMean,$_orderDeliveryTypeMean,$_SupplyOrderCfg;
        $id = $this->_request->getParam('id',0);
        if(empty($id)){
            $this->redirect ( '/user//buylist' );
        }


        $order = new SaleorderModel();
        $orderinfo = $order->getSaleOrderDetail("sale_order.id={$id}");


        if(empty($orderinfo['business_name'])){
            if($orderinfo['buy_sku_type']==$_saleOrderType['Own']) {
                $validate = array(
                    'id' => $orderinfo['buy_sku_id'],
                );

                $url = _HTTP_CLOUD_HOST . "/api/getprice/getQuoteById";
                $prodcut = Tools::https_request($url, $validate);
                $getlist = json_decode($prodcut, true);
                if (!empty($getlist) && $getlist['status'] == 'success') {
                    $orderinfo['business_name'] = '上海塑米信息科技有限公司';
                    $orderinfo['contacts'] = $getlist['data']['realname'];
                    $orderinfo['mobile'] = $getlist['data']['mobile'];
                } else {
                    $orderinfo['business_name'] = '上海塑米信息科技有限公司';
                    $orderinfo['contacts'] = "冯丽姣";
                    $orderinfo['mobile'] = "021-61657142";
                }
            }else{

                $quota = new QuotationModel();
                $quotainfo = $quota->getListById("iq_quotation.id='{$orderinfo['buy_sku_id']}'");

                $orderinfo['business_name'] = $quotainfo['business_name'];
                $orderinfo['contacts'] = $quotainfo['contacts'];
                $orderinfo['mobile'] = $quotainfo['mobile'];
                $orderinfo['qq'] = $quotainfo['qq'];
            }
        }

        $this->_view->_supplyOrderCfg = $_SupplyOrderCfg;
        $this->_view->_orderDeliveryTypeMean=$_orderDeliveryTypeMean;
        $this->_view->_SupplyOrderMean = $_SupplyOrderMean;
        $this->_view->orderinfo =$orderinfo;


    }

    //免费试样
    public function sampleAction(){
        $this->_title ="免费试样";
        $this->_view->title = $this->_title;
        global $_SampleStateMean,$_SampleStateCfg;
        define ( 'ITEM_PER_PAGE', 10 );
        $pageurl="/user/sample";
        $condition = array();

        $status = $this->_request->getParam('status','audit');

        if(!empty($status)){
            if($status == 'audit'){
                $condition[] = "free_sample.status='{$_SampleStateCfg['audit_delivery']}'";
            }elseif($status == 'wait'){
                $condition[] ="free_sample.status='{$_SampleStateCfg['wait_delivery']}'";
            }elseif($status == 'have'){
                $condition[] = "free_sample.status='{$_SampleStateCfg['have_delivery']}'";
            }elseif($status == 'done'){
                $condition[] = "free_sample.status='{$_SampleStateCfg['done_delivery']}'";
            }else{
                $condition[] = "free_sample.status='{$_SampleStateCfg['audit_delivery']}'";
            }
            $pageurl .="/status/".$status;
        }



        if(!empty($condition))
        {
            $where = implode('AND ', $condition);
        }else{
            $where = " 1=1 ";
        }

        $orderby ="update_time desc";

        $sample = new SampleskuModel();
        $totalItems = $sample->getSampleOrderTotal($where);

        // 获取分页开始
        $page = $this->_request->getParam ( "page", 1 );
        $itempage = $this->_request->getParam ( "itempage", 0 );

        $offset = ($page - 1) * ITEM_PER_PAGE;
        if ($offset >= $totalItems) {
            $page = ceil ( $totalItems / ITEM_PER_PAGE );
            $offset = ($page - 1) * ITEM_PER_PAGE;
        }
        if ($offset < 0) {
            $offset = 0;
            $page = 1;
        }


        if(!empty($page)){
            $pageurl .= "/page/%d";
        }

        $list = $sample->getSampleOrderList($page,ITEM_PER_PAGE, $where,$orderby);

        $links = Tools::createPage($pageurl, ITEM_PER_PAGE, $page, $totalItems, $target = '_self');


        $this->_view->links = $links;
        $this->_view->_SampleStateMean =$_SampleStateMean;
        $this->_view->list = $list;
        $this->_view->status = $status;
    }

    //免费试样详情
    public function sampledetailAction(){
        $this->_title ="免费试样详情";
        $this->_view->title = $this->_title;
        global $_SampleStateMean,$_SampleStateCfg;
        $id = $this->_request->getParam('id',0);
        if(empty($id)){
            $this->redirect ( '/user/sample' );
        }

        $where ="free_sample.id =".$id;
        $sample = new SampleskuModel();
        $sampledetail = $sample->getSampleOrderDetail($where);



        $region = new RegionModel();
        $province = $region->getRowByWhere("region_pid='{$sampledetail['province_id']}'");
        $city = $region->getRowByWhere("region_pid='{$sampledetail['city_id']}'");
        $area= $region->getRowByWhere("region_pid='{$sampledetail['area_id']}'");

        $sampledetail['province_id'] = $province['region_name'];
        $sampledetail['city_id'] = $city['region_name'];
        $sampledetail['area_id'] = $area['region_name'];


        //开始读取订单物流
        if(!empty($sampledetail['bill_sn']) || !empty($sampledetail['logistics_company'])){
            $AppKey = '26e50adaf7c127ac';
            $url = 'http://api.kuaidi100.com/api?id=' . $AppKey . '&com=' . $sampledetail['logistics_company'] . '&nu=' . $sampledetail['bill_sn'] . '&show=0&muti=1&order=asc';
            $getkaidi = Tools::https_request($url, array('v'=>1));
            $get_content = json_decode($getkaidi,true);
        }

        $this->_view->get_content =empty($get_content['data']) ? '' : $get_content['data'];
        $this->_view->sampledetail = $sampledetail;
        $this->_view->_SampleStateMean =$_SampleStateMean;
        $this->_view->_SampleStateCfg =$_SampleStateCfg;
    }

    //会员信息
    public function userinfosAction(){

        $this->_title ="会员信息";
        $this->_view->title = $this->_title;



        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['realname']          =   empty($Post['realname']) ? '' : htmlspecialchars(trim($Post['realname']), ENT_QUOTES, 'UTF-8');
            $Post['email']             =   empty($Post['email']) ? '' : htmlspecialchars(trim($Post['email']), ENT_QUOTES, 'UTF-8');
            $Post['moblie']            =   empty($Post['moblie']) ? '' : htmlspecialchars(trim($Post['moblie']), ENT_QUOTES, 'UTF-8');
            $Post['tel']               =   empty($Post['tel']) ? '' : htmlspecialchars(trim($Post['tel']), ENT_QUOTES, 'UTF-8');
            $Post['qq']                =   empty($Post['qq']) ? '' : htmlspecialchars(trim($Post['qq']), ENT_QUOTES, 'UTF-8');
            $Post['sex']                =   empty($Post['sex']) ? '' : htmlspecialchars(trim($Post['sex']), ENT_QUOTES, 'UTF-8');


            $Post['province_id']       =   empty($Post['province_id']) ? 0 : (int)$Post['province_id'];
            $Post['city_id']           =   empty($Post['city_id']) ? 0 : (int)$Post['city_id'];
            $Post['area_id']           =   empty($Post['area_id']) ? 0 : (int)$Post['area_id'];
            $Post['detail_addr']           =   empty($Post['detail_addr']) ? '' : htmlspecialchars($Post['detail_addr'],3,'UTF-8');

            $up_data = array();

            if(!empty($Post['realname'])){
                $up_data['realname'] = $Post['realname'];
            }
            if(!empty($Post['email'])){
                $up_data['email'] = $Post['email'];
            }
            if(!empty($Post['moblie'])){
                $up_data['moblie'] = $Post['moblie'];
            }
            if(!empty($Post['tel'])){
                $up_data['tel'] = $Post['tel'];
            }
            if(!empty($Post['qq'])){
                $up_data['qq'] = $Post['qq'];
            }

            if(!empty($Post['sex'])){
                $up_data['sex'] = $Post['sex'];
            }


            if(!empty($Post['province_id'])){
                $up_data['province_id'] = $Post['province_id'];
            }
            if(!empty($Post['city_id'])){
                $up_data['city_id'] = $Post['city_id'];
            }
            if(!empty($Post['area_id'])){
                $up_data['area_id'] = $Post['area_id'];
            }
            if(!empty($Post['detail_addr'])){
                $up_data['detail_addr'] = $Post['detail_addr'];
            }

            if(isset($up_data)){
                $user = new UserModel();
                $ret = $user->updateById($this->_logininfo['id'],$up_data);
                if(empty($ret)){
                    Tools::echoJs('alert("未有任何更新!");');
                }else{
                    //这边更新session信息
                    if( empty($_SESSION['loginInfo']) ) {
                        $_SESSION['loginInfo']  = array();
                    }
                    $retUser = $user->getListById($this->_logininfo['id']);
                    if(!empty($retUser['realname'])){
                        $loginInfo = $this->_session->offsetGet('loginInfo');
                        $loginInfo['signature']=$retUser['realname'];
                        $this->_session->offsetSet('loginInfo',$loginInfo);
                    }else if(!empty($retUser['moblie'])){
                        $loginInfo = $this->_session->offsetGet('loginInfo');
                        $loginInfo['signature']=$retUser['moblie'];
                        $this->_session->offsetSet('loginInfo',$loginInfo);
                    }else{
                        $loginInfo = $this->_session->offsetGet('loginInfo');
                        $loginInfo['signature']=$retUser['login_name'];
                        $this->_session->offsetSet('loginInfo',$loginInfo);
                    }
                    Tools::echoJs('alert("更新成功!");');
                }
            }
        }

        $user = new UserModel();
        $userinfo = $user->getRowByWhere("id={$this->_logininfo['id']}");

        $region = new RegionModel();
        $province = $region->getListByRow("region_pid",0,"region_pid desc");
        $this->_view->province = $province;



        $userinfo['province_name'] = $region->getOneByIdAndName($userinfo['province_id'],"region_name");
        $userinfo['city_name'] = $region->getOneByIdAndName($userinfo['city_id'],"region_name");
        $userinfo['area_name'] = $region->getOneByIdAndName($userinfo['area_id'],"region_name");

        $this->_view->userinfo = $userinfo;
    }

    //公司信息
    public function companyAction(){
        $this->_title ="公司信息";
        $this->_view->title = $this->_title;
        $ui = new UsersinvoiceModel();
        $cpl = $ui->getListByRow("user_id", $this->_logininfo['id'],$orderby="id desc");
        $this->_view->cpl = $cpl;
    }

    //新增公司
    public function addcompanyAction(){
        $this->_title ="新增公司信息";
        $this->_view->title = $this->_title;

        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();

            $Post['business_name']          =   empty($Post['business_name']) ? '0' : htmlspecialchars(trim($Post['business_name']), ENT_QUOTES, 'UTF-8');
            $Post['business_remarks']             =   empty($Post['business_remarks']) ? '0' : htmlspecialchars(trim($Post['business_remarks']), ENT_QUOTES, 'UTF-8');
            $Post['business_phone']            =   empty($Post['business_phone']) ? '0' : htmlspecialchars(trim($Post['business_phone']), ENT_QUOTES, 'UTF-8');
            $Post['main_variety']               =   empty($Post['main_variety']) ? '0' : htmlspecialchars(trim($Post['main_variety']), ENT_QUOTES, 'UTF-8');
            $Post['contacts']                =   empty($Post['contacts']) ? '0' : htmlspecialchars(trim($Post['contacts']), ENT_QUOTES, 'UTF-8');
            $Post['mobile']                =   empty($Post['mobile']) ? '0' : htmlspecialchars(trim($Post['mobile']), ENT_QUOTES, 'UTF-8');
            $Post['phone']                =   empty($Post['phone']) ? '0' : htmlspecialchars(trim($Post['phone']), ENT_QUOTES, 'UTF-8');
            $Post['email']                =   empty($Post['email']) ? '0' : htmlspecialchars(trim($Post['email']), ENT_QUOTES, 'UTF-8');
            $Post['qq']                =   empty($Post['qq']) ? '0' : htmlspecialchars(trim($Post['qq']), ENT_QUOTES, 'UTF-8');

            $up_data = array();
            $up_data['user_id'] = $this->_logininfo['id'];

            if(!empty($Post['business_name'])){
                $up_data['business_name'] = $Post['business_name'];
            }else{
                Tools::echoJs("alert('请输入公司名称！')");
            }
            if(!empty($Post['business_remarks'])){
                $up_data['business_remarks'] = $Post['business_remarks'];
            }else{
                Tools::echoJs("alert('请输入公司简称！')");
            }
            if(!empty($Post['business_phone'])){
                $up_data['business_phone'] = $Post['business_phone'];
            }else{
                Tools::echoJs("alert('请输入公司电话！')");
            }
            if(!empty($Post['main_variety'])){
                $up_data['main_variety'] = $Post['main_variety'];
            }else{
                Tools::echoJs("alert('请输入主营产品！')");
            }
            if(!empty($Post['contacts'])){
                $up_data['contacts'] = $Post['contacts'];
            }else{
                Tools::echoJs("alert('请输入联系人！')");
            }
            if(!empty($Post['mobile'])){
                $up_data['mobile'] = $Post['mobile'];
            }else{
                Tools::echoJs("alert('请输入手机号码！')");
            }
            if(!empty($Post['phone'])){
                $up_data['phone'] = $Post['phone'];
            }
            if(!empty($Post['email'])){
                $up_data['email'] = $Post['email'];
            }
            if(!empty($Post['qq'])){
                $up_data['qq'] = $Post['qq'];
            }

            $ui = new UsersinvoiceModel();
            $isCount = $ui->getCountByWhere("user_id='{$this->_logininfo['id']}'");
            $isExist = $ui->getCountByWhere("business_name='{$up_data["business_name"]}'");
            if(empty($isExist)) {
                if ($isCount < _ADD_USER_CONPANY_NUM) {
                    $isdefault = $ui->getRowByWhere("is_default='Y'");
                    if (empty($isdefault)) {
                        $up_data['is_default'] = 'Y';
                    }
                    $ret = $ui->add($up_data);
                    if (empty($ret)) {
                        Tools::echoJs("alert('新增公司失败！')");
                    } else {
                        $this->redirect('/user/company');
                    }
                } else {
                    Tools::echoJs("alert('新增公司不能超过5家！')");
                }
            }else{
                Tools::echoJs("alert('该公司已经存在！')");
            }
        }
    }

    //编辑公司
    public function editcompanyAction(){
        $this->_title ="编辑公司信息";
        $this->_view->title = $this->_title;

        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['id']          =   empty($Post['id']) ? '0' : (int)$Post['id'];
            $Post['business_name']          =   empty($Post['business_name']) ? '0' : htmlspecialchars(trim($Post['business_name']), ENT_QUOTES, 'UTF-8');
            $Post['business_remarks']             =   empty($Post['business_remarks']) ? '0' : htmlspecialchars(trim($Post['business_remarks']), ENT_QUOTES, 'UTF-8');
            $Post['business_phone']            =   empty($Post['business_phone']) ? '0' : htmlspecialchars(trim($Post['business_phone']), ENT_QUOTES, 'UTF-8');
            $Post['main_variety']               =   empty($Post['main_variety']) ? '0' : htmlspecialchars(trim($Post['main_variety']), ENT_QUOTES, 'UTF-8');
            $Post['contacts']                =   empty($Post['contacts']) ? '0' : htmlspecialchars(trim($Post['contacts']), ENT_QUOTES, 'UTF-8');
            $Post['mobile']                =   empty($Post['mobile']) ? '0' : htmlspecialchars(trim($Post['mobile']), ENT_QUOTES, 'UTF-8');
            $Post['phone']                =   empty($Post['phone']) ? '0' : htmlspecialchars(trim($Post['phone']), ENT_QUOTES, 'UTF-8');
            $Post['email']                =   empty($Post['email']) ? '0' : htmlspecialchars(trim($Post['email']), ENT_QUOTES, 'UTF-8');
            $Post['qq']                =   empty($Post['qq']) ? '0' : htmlspecialchars(trim($Post['qq']), ENT_QUOTES, 'UTF-8');

            $up_data = array();

            if(!empty($Post['business_name'])){
                $up_data['business_name'] = $Post['business_name'];
            }else{
                Tools::echoJs("alert('请输入公司名称！')");
            }
            if(!empty($Post['business_remarks'])){
                $up_data['business_remarks'] = $Post['business_remarks'];
            }else{
                Tools::echoJs("alert('请输入公司简称！')");
            }
            if(!empty($Post['business_phone'])){
                $up_data['business_phone'] = $Post['business_phone'];
            }else{
                Tools::echoJs("alert('请输入公司电话！')");
            }
            if(!empty($Post['main_variety'])){
                $up_data['main_variety'] = $Post['main_variety'];
            }else{
                Tools::echoJs("alert('请输入主营产品！')");
            }
            if(!empty($Post['contacts'])){
                $up_data['contacts'] = $Post['contacts'];
            }else{
                Tools::echoJs("alert('请输入联系人！')");
            }
            if(!empty($Post['mobile'])){
                $up_data['mobile'] = $Post['mobile'];
            }else{
                Tools::echoJs("alert('请输入手机号码！')");
            }
            if(!empty($Post['phone'])){
                $up_data['phone'] = $Post['phone'];
            }
            if(!empty($Post['email'])){
                $up_data['email'] = $Post['email'];
            }
            if(!empty($Post['qq'])){
                $up_data['qq'] = $Post['qq'];
            }

            $ui = new UsersinvoiceModel();

            $ret = $ui->updateById($Post['id'],$up_data);
            if (empty($ret)) {
                Tools::echoJs("alert('更新公司信息失败！')");
            } else {
                $this->redirect('/user/company');
            }

        }


        $id = $this->_request->getParam('id',0);
        if(empty($id)){
            $this->redirect('/user/company');
        }
        $ui = new UsersinvoiceModel();
        $userinvoice  = $ui->getRowByWhere("id='{$id}'");

        $this->_view->userinvoice = $userinvoice;
    }

    //密码修改
    public function passwordAction(){
        $this->_title ="密码修改";
        $this->_view->title = $this->_title;
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();

            $Post['password'] = empty($Post['password']) ? 0 : htmlspecialchars(trim($Post['password']) , ENT_QUOTES ,'utf-8');
            $Post['original_password'] = empty($Post['original_password']) ? 0 : htmlspecialchars(trim($Post['original_epassword']) , ENT_QUOTES ,'utf-8');
            $Post['confirm_password'] = empty($Post['confirm_password']) ? 0 : htmlspecialchars(trim($Post['confirm_epassword']) , ENT_QUOTES ,'utf-8');

            $user = new UserModel();
            $user->updateById($this->_logininfo['id'],"password ='{$Post['password']}'");
            md5($Post['original_password']);
        }
    }

    //TODO 会员功能操作如下：
    //安全中心
    public function securityAction(){
        $this->_title ="安全中心";
        $this->_view->title = $this->_title;
    }

    //设置默认公司
    public function setdefaultAction(){
        $inv_id = $this->_request->getParam ( "inv_id", 0 );
        $ui = new UsersinvoiceModel();


        //先更新所有默认
        $retdata = $ui->updateByWhere("user_id={$this->_logininfo['id']}", array('is_default'=>'N'));
        $ret = $ui->updateById($inv_id, array('is_default'=>'Y'));

        //在指定更新内容

        if(!empty($ret)){
            dieJs('alert("设置成功!");window.history.go(-1)');
        }else{
            dieJs('alert("设置不成功!");window.history.go(-1)');
        }
    }


    //报价管理 -- 下架
    public function supplydownAction(){
        global $_suppliyStateCfg;
        $id = $this->_request->getParam('id',0);

        if(empty($id)){
            Tools::echoJs("alert('参数不正确！');history.back();");
            die();
        }

        $supply = new QuotationModel();
        $supplydata = $supply->getListById("iq_quotation.id={$id}");
        if ($supplydata['status'] == $_suppliyStateCfg['up']) {
            $data = array(
                'update_time' => $this->_daytime,
                'status' => $_suppliyStateCfg['down']
            );
            $ret = $supply->updateById($id, $data);
            if(!empty($ret)){
                Tools::echoJs("alert('下架成功！');history.back();");
            }
        }

        die();
    }

    //报价管理 -- 上架
    public function supplyupAction(){
        global $_suppliyStateCfg;
        $id = $this->_request->getParam('id',0);

        if(empty($id)){
            Tools::echoJs("alert('参数不正确！');history.back();");
            die();
        }

        $supply = new QuotationModel();
        $supplydata = $supply->getListById("iq_quotation.id={$id}");
        if ($supplydata['status'] == $_suppliyStateCfg['down']) {
            $data = array(
                'update_time' => $this->_daytime,
                'status' => $_suppliyStateCfg['up']
            );
            $ret = $supply->updateById($id, $data);
            if(!empty($ret)){
                Tools::echoJs("alert('上架成功！');history.back();");
            }
        }
        die();
    }

    //报价管理 -- 更新
    public function supplyupdateAction(){
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            global $_suppliyStateMean, $_suppliyStateCfg;

            $Post['id'] = empty($Post['id']) ? '0' : (int) $Post['id'];
            $Post['price'] = empty($Post['price']) ? '0' : htmlspecialchars(trim($Post['price']), ENT_QUOTES, 'utf-8');
            $Post['num'] = empty($Post['num']) ? '0' : htmlspecialchars(trim($Post['num']), ENT_QUOTES, 'utf-8');

            if(empty($Post['id'])){
                $code = array(
                    'code'=>'2',
                    'msg'=>"产品没有选中!"
                );
                echo json_encode($code);die();
            }

            if(empty($Post['price'])){
                $code = array(
                    'code'=>'2',
                    'msg'=>"产品价格不能为空!"
                );
                echo json_encode($code);die();
            }

            if(empty($Post['num'])){
                $code = array(
                    'code'=>'2',
                    'msg'=>"产品数量不能为空!"
                );
                echo json_encode($code);die();
            }

            $supply = new QuotationModel();
            $supplydata = $supply->getListById("iq_quotation.id={$Post['id']}");

            if ($supplydata['status'] != $_suppliyStateCfg['up']) {
                $data = array(
                    'price'        => $Post['price'],
                    'num'          => $Post['num'],
                    'update_time'  => $this->_daytime,
                    'status'       => $_suppliyStateCfg['audit']
                );
                $ret = $supply->updateById($Post['id'],$data);
            }else{
                $data = array(
                    'price'        => $Post['price'],
                    'num'          => $Post['num'],
                    'update_time'  => $this->_daytime,
                );
                $ret = $supply->updateById($Post['id'],$data);
            }

            if(!empty($ret)){
                $code = array(
                    'code'=>'8',
                    'msg'=>"更新成功!"
                );
                echo json_encode($code);die();
            }

            die();
        }
        die();
    }

    //报价管理 -- 取消
    public function supplycancelAction(){
        global $_suppliyStateCfg;
        $id = $this->_request->getParam('id',0);

        if(empty($id)){
            Tools::echoJs("alert('参数不正确！');history.back();");
            die();
        }

        $supply = new QuotationModel();
        $supplydata = $supply->getListById("iq_quotation.id={$id}");
        if ($supplydata['status'] == $_suppliyStateCfg['audit']) {
            $data = array(
                'update_time' => $this->_daytime,
                'status' => $_suppliyStateCfg['cancel']
            );
            $ret = $supply->updateById($id, $data);
            if(!empty($ret)){
                Tools::echoJs("alert('取消成功！');history.back();");
            }
        }
        die();
    }

    //报价管理 -- 删除
    public function supplydelAction(){
        global $_suppliyStateCfg;
        $id = $this->_request->getParam('id',0);

        if(empty($id)){
            Tools::echoJs("alert('参数不正确！');history.back();");
            die();
        }

        $supply = new QuotationModel();
        $supplydata = $supply->getListById("iq_quotation.id={$id}");
        if ($supplydata['status'] == $_suppliyStateCfg['cancel']) {

            $ret = $supply->delete($id);
            if(!empty($ret)){
                $this->redirect ( '/user/supplylist' );
            }
        }
        die();
    }

    //求购管理 -- 取消
    public function buycancelAction(){
        global $_BuyStateCfg;
        $id = $this->_request->getParam('id',0);

        if(empty($id)){
            Tools::echoJs("alert('参数不正确！');history.back();");
            die();
        }

        $buy = new BuyModel();
        $buydata = $buy->getListById("iq_inquiry.id={$id}");
        if ($buydata['status'] == $_BuyStateCfg['wait_audit']) {
            $data = array(
                'update_time' => $this->_daytime,
                'status' => $_BuyStateCfg['cancel']
            );
            $ret = $buy->updateById($id, $data);
            if(!empty($ret)){
                $this->redirect ( '/user/buylist' );
            }
        }
        die();
    }

    //报价意向单  -- 取消
    public function supplyintentcancelAction(){
        global $_BidPurchaseMean,$_BidPurchaseCfg;
        $id = $this->_request->getParam('id',0);

        if(empty($id)){
            Tools::echoJs("alert('参数不正确！');history.back();");
            die();
        }

        $quota = new QuotationModel();
        $buydata = $quota->getListById("iq_quotation.id={$id}");

        if ($buydata['status'] == $_BidPurchaseCfg['audit'] || $buydata['status'] == $_BidPurchaseCfg['order']) {
            $data = array(
                'update_time' => $this->_daytime,
                'status' => $_BidPurchaseCfg['cancel']
            );
            $ret = $quota->updateById($id, $data);
            if(!empty($ret)){
                $this->redirect ( '/user/supplyintent' );
            }
        }
        die();
    }

    //销售订单   -- 订单确认  注：确认订单时候可以改动订单的价格及数量，改动后状态带交易中
    public function confirorderAction(){
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            global $_SupplyOrderMean, $_SupplyOrderCfg;

            $Post['id'] = empty($Post['id']) ? '0' : (int) $Post['id'];
            $Post['price'] = empty($Post['price']) ? '0' : htmlspecialchars(trim($Post['price']), ENT_QUOTES, 'utf-8');
            $Post['num'] = empty($Post['num']) ? '0' : htmlspecialchars(trim($Post['num']), ENT_QUOTES, 'utf-8');

            if(empty($Post['id'])){
                $code = array(
                    'code'=>'2',
                    'msg'=>"产品没有选中!"
                );
                echo json_encode($code);die();
            }

            if(empty($Post['price'])){
                $code = array(
                    'code'=>'2',
                    'msg'=>"产品价格不能为空!"
                );
                echo json_encode($code);die();
            }

            if(empty($Post['num'])){
                $code = array(
                    'code'=>'2',
                    'msg'=>"产品数量不能为空!"
                );
                echo json_encode($code);die();
            }



            $order = new SaleorderModel();
            $orderinfo = $order->getSaleOrderDetail("sale_order.id=".$Post['id']);
            $ordersku = new SaleorderskuModel();


            if ($orderinfo['status'] == $_SupplyOrderCfg['audit']) {
                $data = array(
                    'update_time'  => $this->_daytime,
                    'status'       => $_SupplyOrderCfg['deal']
                );
                $ret = $order->updateById($Post['id'],$data);
                $ordersku->updateByWhere("order_id=".$Post['id'],array('price'=>$Post['price'],'num'=> $Post['num'],));
            }

            if(!empty($ret)){
                $code = array(
                    'code'=>'8',
                    'msg'=>"确认成功!"
                );
                echo json_encode($code);die();
            }

            die();
        }
        die();
    }

    //销售订单   -- 交易完成  注：确认后的订单转线下操作。其中状态只要交易完成就可以。
    public function salesorderdoneAction(){
        global $_SupplyOrderCfg;
        $id = $this->_request->getParam('id',0);

        if(empty($id)){
            Tools::echoJs("alert('参数不正确！');history.back();");
            die();
        }

        $order = new SaleorderModel();
        $orderinfo = $order->getSaleOrderDetail("sale_order.id=".$id);
        if ($orderinfo['status'] == $_SupplyOrderCfg['deal']) {
            $data = array(
                'update_time' => $this->_daytime,
                'status' => $_SupplyOrderCfg['done']
            );
            $ret = $order->updateById($id, $data);
            if(!empty($ret)){
                Tools::echoJs("alert('确认交易成功！');history.back();");
            }
        }
        die();
    }

    //销售订单   --  订单取消
    public function saleordercancelAction(){
        global $_SupplyOrderCfg;
        $id = $this->_request->getParam('id',0);

        if(empty($id)){
            Tools::echoJs("alert('参数不正确！');history.back();");
            die();
        }

        $order = new SaleorderModel();
        $orderinfo = $order->getSaleOrderDetail("sale_order.id=".$id);
        if ($orderinfo['status'] == $_SupplyOrderCfg['audit'] || $orderinfo['status'] == $_SupplyOrderCfg['deal']) {
            $data = array(
                'update_time' => $this->_daytime,
                'status' => $orderinfo['cancel']
            );
            $ret = $order->updateById($id, $data);
            if(!empty($ret)){
                $this->redirect ( '/user/saleorder' );
            }
        }
        die();
    }

    //采购订单   --  订单取消
    public function purchaseordercancelAction(){
        global $_SupplyOrderCfg;
        $id = $this->_request->getParam('id',0);

        if(empty($id)){
            Tools::echoJs("alert('参数不正确！');history.back();");
            die();
        }

        $order = new SaleorderModel();
        $orderinfo = $order->getSaleOrderDetail("sale_order.id=".$id);
        if ($orderinfo['status'] == $_SupplyOrderCfg['audit'] || $orderinfo['status'] == $_SupplyOrderCfg['deal']) {
            $data = array(
                'update_time' => $this->_daytime,
                'status' => $orderinfo['cancel']
            );
            $ret = $order->updateById($id, $data);
            if(!empty($ret)){
                $this->redirect ( '/user/purchaseorder' );
            }
        }
        die();
    }

    //免费试样订单   --  订单取消
    public function sampleordercancelAction(){
        global $_SampleStateMean,$_SampleStateCfg;
        $id = $this->_request->getParam('id',0);

        if(empty($id)){
            Tools::echoJs("alert('参数不正确！');history.back();");
            die();
        }

        $sample = new SampleModel();
        $orderinfo = $sample->getRowByWhere("free_sample.id=".$id);
        if ($orderinfo['status'] == $_SampleStateCfg['audit_delivery']) {
            $data = array(
                'update_time' => $this->_daytime,
                'status' => $_SampleStateCfg['cancel_delivery']
            );
            $ret = $sample->updateById($id, $data);
            if(!empty($ret)){
                $this->redirect ( '/user/sample' );
            }
        }
        die();
    }

    //关于我们
    public function aboutAction(){
        $this->_title ="关于我们";
        $this->_view->title = $this->_title;
    }

    //设置中心
    public function setupAction(){
        $this->_title ="设置中心";
        $this->_view->title = $this->_title;
    }


}